package com.healthcloud.service.impl;

import com.healthcloud.dao.TestresultDao;
import com.healthcloud.entity.PatientRecord.RecordInfoVo;
import com.healthcloud.entity.TestresultEntity;
import com.healthcloud.entity.testresult.TestResultDto;
import com.healthcloud.entity.testresult.TestresultVo;
import com.healthcloud.service.TestresultService;
import com.healthcloud.utils.PageParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service("testresultService")
@Transactional
public class TestresultServiceImpl implements TestresultService {
    @Autowired
    private TestresultDao testresultDao;

    @Override
    public void saveBloodSugar(TestresultEntity testresult) {
        testresult.setTestKindCode("105");
        testresult.setTestKindName("血糖");
        testresult.setDataType(20);
        testresult.setIsPublish(1);
        testresult.setCreateTime(new Date());

        if ("105001".equals(testresult.getTestItemCode())) {
            testresult.setTestItemName("空腹血糖");
        } else if ("105002".equals(testresult.getTestItemCode())) {
            testresult.setTestItemName("餐后2小时血糖");
        }
        save(testresult);
    }

    @Override
    public void saveBloodPressure(String idCard, Double szyValue, Double ssyValue, Double heartRate, Date testTime) {
        TestresultEntity entity = new TestresultEntity();
        entity.setTestTime(testTime);
        entity.setIdCard(idCard);
        entity.setTestKindCode("104");
        entity.setTestKindName("血压");
        entity.setDataType(20);
        entity.setIsPublish(1);
        entity.setCreateTime(new Date());

        entity.setTestItemCode("104001");
        entity.setTestItemName("收缩压");
        entity.setTestValue(String.valueOf(ssyValue));
        save(entity);

        entity.setTestItemCode("104002");
        entity.setTestItemName("舒张压");
        entity.setTestValue(String.valueOf(szyValue));
        save(entity);

        entity.setTestItemCode("104003");
        entity.setTestItemName("周期脉搏");
        entity.setTestValue(String.valueOf(heartRate));
        save(entity);
    }

    @Override
    public TestresultEntity queryObject(Long id) {
        return testresultDao.queryObject(id);
    }

    @Override
    public List<TestresultEntity> queryList(TestresultEntity bean, PageParam pageParam) {
        return testresultDao.queryList(bean, pageParam);
    }

    @Override
    public int queryTotal(TestresultEntity bean) {
        return testresultDao.queryTotal(bean);
    }

    @Override
    public void save(TestresultEntity testresult) {
        testresultDao.save(testresult);
    }

    @Override
    public void update(TestresultEntity testresult) {
        testresultDao.update(testresult);
    }

    @Override
    public void delete(Long id) {
        testresultDao.delete(id);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        testresultDao.deleteBatch(ids);
    }

    @Override
    public List<TestresultEntity> queryResult(TestresultEntity entity, String companyId, PageParam pageParam) {
        return testresultDao.queryResult(entity, companyId, pageParam);
    }

    @Override
    public int countResult(TestresultEntity entity, String companyId) {
        return testresultDao.countResult(entity, companyId);
    }

    @Override
    public List<TestresultEntity> queryBySN(String mkMedicalInstrumenId) {
        return testresultDao.queryBySN(mkMedicalInstrumenId);
    }

    @Override
    public List<TestresultEntity> queryResultBySN(String mkMedicalInstrumenId, PageParam pageParam) {
        return testresultDao.queryResultBySN(mkMedicalInstrumenId, pageParam);
    }

    @Override
    public int countResultBySN(String mkMedicalInstrumenId) {
        return testresultDao.countResultBySN(mkMedicalInstrumenId);
    }

    @Override
    public List<RecordInfoVo> queryRecord(String idCard, String testKindCode, String beginTime, String endTime, PageParam pageParam) {
        return testresultDao.queryRecord(idCard, testKindCode, beginTime, endTime, pageParam);
    }

    @Override
    public int countRecord(String idCard, String testKindCode, String beginTime, String endTime) {
        return testresultDao.countRecord(idCard, testKindCode, beginTime, endTime);
    }

    @Override
    public List<TestresultEntity> queryLastBp(String idCard) {
        return testresultDao.queryLastBp(idCard);
    }

    @Override
    public String[] queryListTime(String idCard, String beginTime, String endTime, PageParam pageParam) {
        return testresultDao.queryListTime(idCard, beginTime, endTime, pageParam);
    }

    /**
     * 根据身份证号、开始时间和结束时间，查询测试时间
     *
     * @param idCard
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public String[] listTestTimeByIdCardAndTime(String idCard, String beginTime, String endTime) {
        return testresultDao.listTestTimeByIdCardAndTime(idCard, beginTime, endTime);
    }

    @Override
    public List<TestresultEntity> queryByListTime(String idCard, String[] testTime) {
        return testresultDao.queryByListTime(idCard, testTime);
    }

    @Override
    public int countByListTime(String idCard, String[] testTime) {
        return testresultDao.countByListTime(idCard, testTime);
    }

    @Override
    public TestresultEntity queryLastBg(String idCard, Integer testItemCode) {
        return testresultDao.queryLastBg(idCard, testItemCode);
    }

    @Override
    public List<TestresultEntity> queryBgList(String idCard, Integer testItemCode, PageParam pageParam) {
        return testresultDao.queryBgList(idCard, testItemCode, pageParam);
    }

    @Override
    public List<TestresultEntity> searchBgList(String idCard, Integer testKindCode, String beginTime, String endTime, PageParam pageParam) {
        return testresultDao.searchBgList(idCard, testKindCode, beginTime, endTime, pageParam);
    }


    @Override
    public int countBgList(String idCard, Integer testKindCode, String beginTime, String endTime) {
        return testresultDao.countBgList(idCard, testKindCode, beginTime, endTime);
    }

    @Override
    public List queryPatientTests(TestresultVo entity, PageParam pageParam) {
        return testresultDao.queryPatientTests(entity, pageParam);
    }

    @Override
    public List queryPatientBpTests(TestresultVo entity, PageParam pageParam) {
        return testresultDao.queryPatientBpTests(entity, pageParam);
    }

    @Override
    public int queryPatientTestsCount(TestresultVo entity) {
        return testresultDao.queryPatientTestsCount(entity);
    }

    @Override
    public int queryPatientBpTestsCount(TestresultVo entity) {
        return testresultDao.queryPatientBpTestsCount(entity);
    }

    @Override
    public List resultAvgByDay(String idCard, String date, String testItemCodes) {
        return testresultDao.resultAvgByDay(idCard, date, testItemCodes);
    }

    @Override
    public List<TestresultEntity> queryListVo(TestresultVo entity, PageParam pageParam) {
        return testresultDao.queryListVo(entity, pageParam);
    }

    @Override
    public int queryTotalVo(TestresultVo entity) {
        return testresultDao.queryTotalVo(entity);
    }

    @Override
    public List queryPatientBfTests(TestresultVo entity, PageParam pageParam) {
        return testresultDao.queryPatientBfTests(entity, pageParam);
    }

    @Override
    public int queryPatientBfTestsCount(TestresultVo entity) {
        return testresultDao.queryPatientBfTestsCount(entity);
    }

    /**
     * 统计当年血压测试正常结果
     */
    @Override
    public int countPatientBpTestsCurrYear(String hospitalIds, String type) {
        return testresultDao.countPatientBpTestsCurrYear(hospitalIds, type);
    }

    /**
     * 查找血糖记录
     *
     * @param entity
     * @param pageParam
     * @return
     */
    @Override
    public List patientBsTests(TestResultDto entity, PageParam pageParam) {
        return testresultDao.patientBsTests(entity, pageParam);
    }

    @Override
    public int coutPatientBsTests(TestResultDto entity) {
        return testresultDao.coutPatientBsTests(entity);
    }

    /**
     * 查找血压记录
     *
     * @param entity
     * @param pageParam
     * @return
     */
    @Override
    public List patientBpTests(TestResultDto entity, PageParam pageParam) {
        return testresultDao.patientBpTests(entity, pageParam);
    }

    @Override
    public int countPatientBpTests(TestResultDto entity) {
        return testresultDao.coutPatientBpTests(entity);
    }

    /**
     * 修改血糖数据
     *
     * @param id
     * @param testItemCode
     * @param testItemName
     */
    @Override
    public void updateBs(Integer id, String testItemCode, String testItemName) {
        testresultDao.updateBs(id, testItemCode, testItemName);
    }

    /**
     * 检测记录列表
     *
     * @param testResultDto
     * @param pageParam
     * @return
     */
    @Override
    public List findList(TestResultDto testResultDto, PageParam pageParam) {
        return testresultDao.findList(testResultDto, pageParam);
    }

    @Override
    public int countFindList(TestResultDto testResultDto) {
        return testresultDao.countFindList(testResultDto);
    }


    @Override
    public List<TestresultEntity> queryRecordDetail(String idCard, Integer testKindCode, String testTime) {
        return testresultDao.queryRecordDetail(idCard, testKindCode, testTime);
    }
}
